'use strict';
const AV = require('leanengine');

/**
 * 左侧导航数据
 */
exports.getNav = function (req, res, next) {

    new AV.Query('Category').equalTo('parent_id', 0).ascending('order').find().then(function (results) {

        returnJson.code = 200;
        returnJson.data = results;

        return res.json(returnJson);
    }).catch(function (err) {
        next(err);
    });

};

/**
 * 根据分类id查找子分类数据
 */
exports.getList = function (req, res, next) {

    let id = req.param('id');
    if (!id){
        returnJson.code = 200;
        returnJson.data = results;

        return res.json(returnJson);
    }

    (async function () {
        let list = await new AV.Query('Category').equalTo('parent_id', parseInt(id)).ascending('order').find();

        returnJson.code = 200;
        returnJson.data = list;

        return res.json(returnJson);
    })();

};

/**
 * 根据分类id查找同级的所有分类
 * @param req
 * @param res
 * @param next
 */
exports.getBrotherList = function (req, res, next) {

    let id = req.param('id');
    if (!id){
        returnJson.code = 200;
        return res.json(returnJson);
    }

    (async function () {
        let parent = await new AV.Query('Category').equalTo('cat_id', parseInt(id)).first();
        if(!parent){
            returnJson.code = 200;
            return res.json(returnJson);
        }
        let list = await new AV.Query('Category').equalTo('parent_id', parseInt(parent.get('parent_id'))).find();

        returnJson.code = 200;
        returnJson.data = {
            list: list,
            info: parent
        };

        return res.json(returnJson);
    })();

};